#include "Day.h"

const string Day::days_of_week[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Day::Day(const Day & day) {
	this->day_index_ = day.getDayIndex();
	this->appointments_ = day.getAppointments();
}

Day & Day::operator= (const Day & day) {
	if (this != &day) {
		this->day_index_ = day.getDayIndex();
		this->appointments_ = day.getAppointments();
	}

	return *this;
}

void Day::addAppointment(const Appointment & app) {
	appointments_.insert(app);
}

void Day::removeAppointment(const Appointment & app) {
	appointments_.erase(app);
}

const Appointment * Day::find (const int & start_hour, const int & start_min) const {

	for (set<Appointment>::iterator it = getAppointments().begin(); it != getAppointments().end(); it++) {
		if (it->getStartHour() == start_hour && it->getStartMin() == start_min)
			return &(*it);
	}

	return NULL;
}

ostream & printLine(ostream& os, const int & length, char fill) {
	return os << setw(length) << setfill(fill) << fill << endl;
}

ostream &operator<< (ostream& os, const Day & day) {

	const int rcol = 100;
	const int lcol = 10;
	int cur_hour = -1;
	int tmp_hour = -1;
	int i;
	os.setf(ios::fixed,ios::floatfield);
	os << setprecision(2);

	printLine(os,rcol,'-');
	os << setfill(' ');
	os << left << setw(lcol - 2) << Day::days_of_week[day.day_index_] << " | Meetings" << endl;
	//printLine(os,rcol,'-');

	if (day.getAppointments().size() == 0) {
		printLine(os,rcol,'-');
		os << "(no meetings)" << endl;
	}
	else 
	{
		for (set<Appointment>::iterator it = day.getAppointments().begin(); it != day.getAppointments().end(); it++) {
			tmp_hour = it->getStartHour();
			if (tmp_hour > cur_hour) {
				printLine(os, rcol, '-');
				os << right << setw(2) << setfill('0') << tmp_hour << ":00" << right << setw(lcol - 4) << setfill(' ') << "| ";
			} else {
				os << right << setw(lcol - 1) << setfill(' ') << ' ' << "| ";
			}
			cur_hour = tmp_hour;
			os << *it << endl;
			for (i = cur_hour + 1; i < (it->getEndMin() == 0 ? it->getEndHour() : it->getEndHour() + 1); i++) {
				os << setw(2) << setfill('0') << i << ":00" << right << setw(lcol - 4) << setfill(' ') << "| ";
				os << setw(13) << setfill(' ') << ' ' << "..." << endl;
				cur_hour = i;
			}

		}
	}

	printLine(os,rcol,'-');
	os << endl;

	return os;
}